package com.bcqn.controller;

import com.alibaba.fastjson.JSONObject;
import com.bcqn.common.CommonResponse;
import com.bcqn.model.HousingInformation;
import com.bcqn.model.Position;
import com.bcqn.model.vo.JobQueryObj;
import com.bcqn.model.vo.RentingQueryObj;
import com.bcqn.service.JobService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * 求职模块对应的控制层
 *
 * @author liugang
 * @version 1.0
 * @date 2020/5/1 18:24
 */
@RestController
@RequestMapping("/job")
@Slf4j
public class JobController extends CommonController {

    @Resource
    private JobService jobService;


    /**
     * 根据id来进行删除对应的职位信息
     *
     * @param id
     * @return
     */
    @PutMapping("/delete")
    public CommonResponse delete(Long id) {
        if (id != null) {
            // TODO 鉴权 只能删除本人发布的职位
            // TODO 存在性检验
            log.info("添加的对象是param,id={}", JSONObject.toJSONString(id));
            jobService.delete(id);
            return CommonResponse.success();
        } else {
            return CommonResponse.fail("传递的数据为空，请确定出正确的id信息");
        }
    }

    /**
     * 复制动作，对工作岗位的控制
     *
     * @return
     */
    @PutMapping("/hand")
    public CommonResponse hand(@RequestParam("id") Long id) {
        // TODO 存在性检验
        // TODO insert ignore 多次投递只插入一条记录，但不报错
        jobService.delivery(getUserId(), id);
        return CommonResponse.success();
    }

    /**
     * 岗位类型列表
     *
     * @return
     */
    @RequestMapping("/type/list")
    public CommonResponse test() {
        // 运营、产品、市场、技术、职能、设计、其他
        List<String> typeList = Arrays.asList("运营", "产品", "市场", "技术", "职能", "设计", "其他");
        Map<String, List<String>> a = new HashMap<>();
        a.put("typeList", typeList);
        return CommonResponse.success(a);
    }

    /**
     * 发布求职信息
     *
     * @param position
     * @return
     */
    @RequestMapping("/add")
    public CommonResponse add(@RequestBody Position position) {
        if (position != null) {
            position.setUserId(getUserId().intValue());
            log.info("添加的对象是param,position={}", JSONObject.toJSONString(position));
            return CommonResponse.success(jobService.add(position));
        } else {
            return CommonResponse.fail("传入的对象为空");
        }
    }

    /**
     * 职务详情
     *
     * @param id 职务的id属性
     * @return
     */
    @RequestMapping("/detail")
    public CommonResponse detail(@RequestParam Long id) {
        if (id != null) {
            log.info("添加的对变量是param,id={}", JSONObject.toJSONString(id));
            Position position = jobService.detail(id);
            // TODO 存在性校验
            boolean isCollected = jobService.isCollected(getUserId(), id);

            String jsonString = JSONObject.toJSONString(position);
            JSONObject positionInfo = JSONObject.parseObject(jsonString);
            positionInfo.put("isCollected", isCollected);

            return CommonResponse.success(positionInfo);
        } else {
            return CommonResponse.fail("传入的变量为空");
        }

    }

    /**
     * 查询用户对于的数据的列表
     *
     * @param pageNum 页码，从1开始计数，默认1
     * @param pageSize 页大小，默认10，最大1000
     * @param jobType 岗位类型，不传返回全部类型
     * @param recruitType 招聘类型，实习或社招，不传返回全部类型
     * @param keyword 搜索关键词
     * @param sortKey 排序键，createTime或updateTime或maxSalary或minSalary，默认createTime
     * @param order 升降序, 在sortKey不为空时有效, asc或desc或ASC或DESC, 默认DESC
     * @return
     */
    @RequestMapping("/list")
    public CommonResponse list(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(defaultValue = "") String jobType,
            @RequestParam(defaultValue = "") String recruitType,
            @RequestParam(defaultValue = "") String keyword,
            @RequestParam(defaultValue = "createTime") String sortKey,
            @RequestParam(defaultValue = "DESC") String order
            ) {
        log.info("pageNum={}, pageSize={}, jobType={}, recruitType={}, keyword={}, sortKey={}, order={}",
                pageNum, pageSize, jobType, recruitType, keyword, sortKey, order);
        String[] sortKeys = new String[]{"createTime", "updateTime", "maxSalary", "minSalary"};
        if (!Arrays.asList(sortKeys).contains(sortKey)) {
            return CommonResponse.success("不合法的排序键: sortKey="+sortKey);
        }
        // 驼峰转下划线
        sortKey = sortKey.replaceAll("[A-Z]", "_$0").toLowerCase();
        // TODO 更多参数检查

        PageInfo<Position> pageInfo = jobService.getList(pageNum, pageSize, jobType, recruitType, keyword, sortKey, order);

        return CommonResponse.success(pageInfo);
    }

    /**
     * 收藏职务信息
     *
     * @param id     收藏人的id信息
     * @param action
     * @return
     */
    @RequestMapping("/collect")
    public CommonResponse collect(Long id, String action) {
        log.info("添加的对变量是param,id={},action={}", JSONObject.toJSONString(id), JSONObject.toJSONString(action));
        // TODO 存在性检验
        // TODO 重复收藏检验
        if (action != null) {
            if (action.equals("add")) {
                jobService.collect(getUserId(), id);
                return CommonResponse.success();
            } else if (action.equals("del")) {
                jobService.delCollect(getUserId(), id);
                return CommonResponse.success();
            } else {
                return CommonResponse.fail("不合法的动作, action="+action+" ,应为add或del");
            }
        }
        return CommonResponse.fail("该操作信息传递为空");
    }

    @RequestMapping("/getMyCollection")
    public CommonResponse getMyCollection(@RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        return CommonResponse.success(jobService.getMyCollection(getUserId(), pageSize, pageNum));
    }

    @RequestMapping("/getMyPost")
    public CommonResponse getMyPost(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        return CommonResponse.success(jobService.getMyPost(getUserId(), pageSize, pageNum));
    }

    @RequestMapping("/getMyDelivery")
    public CommonResponse getMyDelivery(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        return CommonResponse.success(jobService.getMyDelivery(getUserId(), pageSize, pageNum));
    }
}
